package com.songjg.demo.utils;

import com.songjg.demo.enums.ResultCode;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author Administrator
 */
@Data
@ToString
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = -7264375751490927423L;

    /**
     * 状态码
     */
    private String code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public <T> ResponseResult() {
    }

    public ResponseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseResult ok() {
        return new ResponseResult(ResultCode.OK.getCode(), ResultCode.OK.getMsg());
    }

    public static <T> ResponseResult ok(T data) {
        return new ResponseResult(ResultCode.OK.getCode(), ResultCode.OK.getMsg(), data);
    }

    public static ResponseResult error() {
        return new ResponseResult(ResultCode.ERROR.getCode(), ResultCode.ERROR.getMsg());
    }

    public static ResponseResult error(String code) {
        return new ResponseResult(code, ResultCode.getMsgByCode(code));
    }

    public static ResponseResult error(String code, String msg) {
        return new ResponseResult(code, msg);
    }

    public static <T> ResponseResult error(String code, T data) {
        return new ResponseResult(code, ResultCode.getMsgByCode(code), data);
    }

}
